package fr.norsys.mapper.console.service;

import java.util.Iterator;
import java.util.Collection;

import net.sf.navigator.menu.MenuComponent;
import net.sf.navigator.menu.MenuRepository;
import fr.norsys.mapper.console.model.Application;
import fr.norsys.mapper.console.model.Connection;
import fr.norsys.mapper.console.utils.URLEncoderUtil;

public class MenuServiceImpl implements MenuService {
	public MenuRepository buildMenu(MenuRepository oldRepository,
			Collection applications, Collection connexions, String isAppliModified, String currentAppli) {
        MenuRepository repository = new MenuRepository();
        repository.setDisplayers(oldRepository.getDisplayers());

        MenuComponent conMenu = new MenuComponent();
        conMenu.setName("Connections");
        conMenu.setTitle("Connections");
        conMenu.setLocation("javascript:saveBeforeLoad('"+isAppliModified+"','"+currentAppli+"','connection.do?method=init')");
		repository.addMenu(conMenu);
		
		for(Iterator it=connexions.iterator();it.hasNext();) {
	        MenuComponent mc = new MenuComponent();
	        Connection connection = (Connection) it.next();
	        String name = connection.getName();
	        mc.setName(name);
	        mc.setTitle(name);
	        mc.setLocation("javascript:saveBeforeLoad('"+isAppliModified+"','"+currentAppli+"','connection.do?method=edit&amp;forward=connection&amp;id="+URLEncoderUtil.escape(connection.getId())+"')");
	        mc.setParent(conMenu);
		}

		MenuComponent appliMenu = new MenuComponent();
		appliMenu.setName("Applications");
        appliMenu.setTitle("Applications");
        appliMenu.setLocation("javascript:saveBeforeLoad('"+isAppliModified+"','"+currentAppli+"','application.do?method=init')");
		repository.addMenu(appliMenu);
		for(Iterator it=applications.iterator();it.hasNext();) {
	        MenuComponent mc = new MenuComponent();
	        Application application = (Application) it.next();
	        String name = application.getName();
	        mc.setName(name);
	        mc.setTitle(name);
	        mc.setLocation("javascript:saveBeforeLoad('"+isAppliModified+"','"+currentAppli+"','application.do?method=loadAppli&id="+URLEncoderUtil.escape(application.getId())+"')");
	        mc.setParent(appliMenu);
		}
		return repository;
	}
		public MenuRepository buildConMenu(MenuRepository oldRepository,
				Collection applications, Collection connexions, String isConModified, String con) {
	        MenuRepository repository = new MenuRepository();
	        repository.setDisplayers(oldRepository.getDisplayers());

	        MenuComponent conMenu = new MenuComponent();
	        conMenu.setName("Connections");
	        conMenu.setTitle("Connections");
	        conMenu.setLocation("javascript:saveConBeforeLoad('"+isConModified+"','"+con+"','connection.do?method=init')");
			repository.addMenu(conMenu);
			
			for(Iterator it=connexions.iterator();it.hasNext();) {
		        MenuComponent mc = new MenuComponent();
		        Connection connection = (Connection) it.next();
		        String name = connection.getName();
		        mc.setName(name);
		        mc.setTitle(name);
		        mc.setLocation("javascript:saveConBeforeLoad('"+isConModified+"','"+con+"','connection.do?method=edit&amp;forward=connection&amp;id="+URLEncoderUtil.escape(connection.getId())+"')");
		        mc.setParent(conMenu);
			}

			MenuComponent appliMenu = new MenuComponent();
			appliMenu.setName("Applications");
	        appliMenu.setTitle("Applications");
	        appliMenu.setLocation("javascript:saveConBeforeLoad('"+isConModified+"','"+con+"','application.do?method=init')");
			repository.addMenu(appliMenu);
			for(Iterator it=applications.iterator();it.hasNext();) {
		        MenuComponent mc = new MenuComponent();
		        Application application = (Application) it.next();
		        String name = application.getName();
		        mc.setName(name);
		        mc.setTitle(name);
		        mc.setLocation("javascript:saveConBeforeLoad('"+isConModified+"','"+con+"','application.do?method=loadAppli&id="+URLEncoderUtil.escape(application.getId())+"')");
		        mc.setParent(appliMenu);
			}
			return repository;
		}
}